package com.yunhe.rentcars.pro.service.permi.impl;


import com.yunhe.rentcars.commons.annoaction.Log;
import com.yunhe.rentcars.commons.util.StringUtils;
import com.yunhe.rentcars.pro.dao.permi.RoleDao;
import com.yunhe.rentcars.commons.dto.Pageable;
import com.yunhe.rentcars.pro.entity.permi.Role;
import com.yunhe.rentcars.pro.service.permi.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2017/11/10 0010.
 */
@Transactional
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Log(value = "添加角色(sys_role)",type = 1)
    @Override
    public void save(Role role, String permissionIds) {

        roleDao.save(role);

        Long roleid = role.getRoleid();

        if(null != roleid && StringUtils.notEmpty(permissionIds)){
            String[] permisIds = permissionIds.split(",");
            Long[] permisIdArr = new Long[permisIds.length];
            for(int i = 0;i<permisIds.length;i++){
                permisIdArr[i] = new Long(permisIds[i]);
            }
            this.saveRolePermission(roleid,permisIdArr);

        }
    }


    @Override
    public void delete(Long roleid) {
        roleDao.delete(roleid);
        roleDao.deleteRolePermission(roleid);
    }

    @Log(value = "删除角色(sys_permission)",type = 2)
    @Override
    public void deleteBatch(String ids) {
        if(StringUtils.notEmpty(ids)){
            String[] idArr = ids.split(",");
            for(String i:idArr){
                this.delete(new Long(i));
            }
        }
    }

    @Log(value = "修改角色(sys_permission)",type = 3)
    @Override
    public void update(Role role, String permissionIds) {

        roleDao.update(role);

        Long roleid = role.getRoleid();
        roleDao.deleteRolePermission(roleid);

        if(null != roleid && StringUtils.notEmpty(permissionIds)){
            String[] permisIds = permissionIds.split(",");
            Long[] permisIdArr = new Long[permisIds.length];
            for(int i = 0;i<permisIds.length;i++){
                permisIdArr[i] = new Long(permisIds[i]);
            }
            this.saveRolePermission(roleid,permisIdArr);
        }
    }

    @Override
    public Role find(Long roleid) {
        Role role = roleDao.find(roleid);
        return role;
    }

    @Override
    public List<Role> findList() {
        List<Role> roleList = roleDao.findList();
        return roleList;
    }

    @Override
    public List<Role> findListCanUse() {
        List<Role> listCanUse = roleDao.findListCanUse();
        return listCanUse;
    }

    @Override
    public Pageable<Role> findPage(Integer offset, Integer limit) {
        List<Role> roleList = roleDao.findPage(offset, limit);
        int count = this.count();
        Pageable<Role> pageable = new Pageable<>();
        pageable.setTotal(count);
        pageable.setRows(roleList);
        return pageable;
    }

    @Override
    public int count() {
        int count = roleDao.count();
        return count;
    }

    @Override
    public void saveRolePermission(Long roleid, Long[] permisIdArr) {
        roleDao.saveRolePermission(roleid,permisIdArr);
    }

    @Override
    public void deleteRolePermission(Long roleid) {
        roleDao.deleteRolePermission(roleid);
    }
}
